''' 员工管理 '''
from ems import emsservice as db
from flask import Blueprint, jsonify, redirect, url_for, request
import logging, requests
import sys, traceback, os, re ,io
import openpyxl
from flask import send_from_directory, make_response



logger = logging.getLogger('app')


ems = Blueprint("ems", __name__, url_prefix="/api")

#获取employee信息
@ems.route('/project/employee_data',methods=['GET'])
def get_emploee_data():
    # 获取查询字符串参数，读取到的参数是字符串类型；用print(type(pageSize),type(pageCurrent),type(searchValue))验证
    logger.info("获取用户档案-雇员页面数据")
    param = {}
    param['pageSize'] = request.args.get('pageSize', default=10, type=int)
    param['pageCurrent']= request.args.get('page', default=1, type=int)
    param['searchValue'] = request.args.get('searchValue', default='', type=str)
    param['selectProjectId'] = request.args.get('selectProject', default=None, type=int)
    param['selectGroupId'] = request.args.get('selectGroup', default=None, type=int)
    result = db.get_employee_data(param)
    return jsonify(result)

#获取选择项目下拉框选项
@ems.route('/project/projectOptions',methods=['GET'])
def get_projectOptions():
    result = db.get_projectOptions()
    return jsonify(result)

#获取选择班组下拉框选项
@ems.route('/project/groupOptions',methods=['GET'])
def get_groupOptions():
    selectProjectId = request.args.get('selectProject', default=None, type=int)
    result = db.get_groupOptions(selectProjectId)
    return jsonify(result)

#创建项目用户
@ems.route('/project/add_employee',methods=['GET','POST'])
def add_employee():
    formdata = request.json
    logger.info(formdata)
    result = db.add_employee(formdata)
    return jsonify(result)

#更新项目用户信息初始化
@ems.route('/project/edit_employee_init',methods=['GET','POST'])
def edit_employee_init():
    args = request.args.to_dict()
    logger.info(args)
    result = db.edit_employee_init(args)
    return jsonify(result)

#更新项目用户信息
@ems.route('/project/update_employee',methods=['GET','POST'])
def update_employee():
    formdata = request.json
    logger.info(formdata)
    result = db.update_employee(formdata)
    return jsonify(result)

#删除employee信息
@ems.route('/project/user/batchDel',methods=['POST'])
def del_employee():
    formdata = request.json
    result = db.del_project_employee(formdata)
    return jsonify(result)

#批量导入人员信息数据
@ems.route('/project/uploadExcel',methods=['POST'])
def uploadExcel():
    file = request.files.get('file')
    f = file.read()
    result = 'ok'
    try:
        # 打开工作薄
        excel_content = io.BytesIO(f)
        excel_file = openpyxl.load_workbook(excel_content)
        ws = excel_file['人员信息']
        db.import_employee(ws)
        excel_file.close()
    except:
        result = 'ng'
        logger.error(sys.exc_info())
    finally:
        return jsonify(result)

#批量导出人员信息数据
@ems.route('/project/getEmpExcel',methods=['GET'])
def getEmpExcel():
    args = request.args.to_dict()
    # 获取模板文件路径
    dir_path = str(os.path.abspath(os.path.dirname(__file__)))
    dirpath = os.path.join(os.path.join(dir_path.replace('\ems',''), 'template'))
    result = db.getEmpExcel(dirpath, args)
    response = make_response(send_from_directory(dirpath, 'export人员信息.xlsx', as_attachment=True))
    response.headers["Access-Control-Expose-Headers"] = "Content-disposition"
    return response






























